package http.secure.common.config;

import http.secure.common.keys.SecureGuidSession;
import http.secure.common.keys.SecureKeyStore;
import http.secure.common.keys.impl.LocalKeyStore;
import http.secure.common.keys.impl.RedisKeyStore;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * 密钥存储自动配置
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class KeyStoreConfiguration {
    private final SecureGuidSession guidSession;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "spring.data.redis.host")
    public SecureKeyStore redisKeyStore(RedisTemplate<String, String> redisTemplate) {
        log.info("Redis key store is enable");
        return new RedisKeyStore(redisTemplate, guidSession);
    }

    @Bean
    @ConditionalOnMissingBean
    public SecureKeyStore sessionKeyStore() {
        log.info("HttpSession key store is enable");
        return new LocalKeyStore(guidSession);
    }
}
